package com.novoda.runbuddy.controller;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.novoda.runbuddy.activity.Stopwatch;
import com.novoda.runbuddy.activity.stopwatch.StopwatchTools;
import com.novoda.runbuddy.provider.ActivityID;
import com.novoda.runbuddy.service.IRemoteStopClockService;
import com.novoda.runbuddy.service.RemoteStopClockServiceInterface;
import com.novoda.runbuddy.view.StopwatchView;
import com.novoda.runbuddy.view.TargetTimeView;

public class StopwatchControllerImpl implements IStopClockController {

	private StopwatchView				stopwatchView;
	private TargetTimeView				stopwatchTargetView;
	protected IRemoteStopClockService	messageService;
	private boolean						targetIsSet	= false;
	private String						targetTime	= "00:00:00.00";

	// public long startTime = 0L;

	public StopwatchControllerImpl(Stopwatch stopwatchActivity) {
	}

	public void createStopClockView(Context activity, TextSwitcher stopClockTextSwitcher, Button startButton) {
		stopwatchView = new StopwatchView(activity, this, stopClockTextSwitcher, startButton);

		stopwatchView.resetStopClock();

	}

	private void updateStopClock() {
		elapsedTime = RemoteStopClockServiceInterface.getInstance().getElapsedTime();
		updateRunningStopClock();
		if (RemoteStopClockServiceInterface.getInstance().isClockRunning()) {
			Message nextUpdateMsg = Message.obtain(updateHandler, ActivityID.UPDATESTOPCLOCK);
			updateHandler.sendMessageDelayed(nextUpdateMsg, 85);
		}
	}

	private Handler	updateHandler	= new Handler()
									{
										public void handleMessage(Message msg) {
											switch (msg.what) {
												case ActivityID.UPDATESTOPCLOCK:
													updateStopClock();
													break;
												case ActivityID.LOCATION_UPDATE:
													break;
											}
											super.handleMessage(msg);
										}

									};

	public Handler getUpdateHandler() {
		return updateHandler;
	}

	public void createwatchClockTargetView(Context context, CheckBox checkBox, TextView hours, TextView minutes, TextView seconds,
			TextView milliseconds, Button hoursUp, Button hoursDown, TextView hoursSep, Button minutesUp, Button minutesDown,
			TextView minutesSep, Button secsUp, Button secsDown, TextView secondsSep, Button millisecsUp, Button millisecsDown) {

		stopwatchTargetView = new TargetTimeView(context, checkBox, hours, minutes, seconds, milliseconds, hoursUp, hoursDown,
				hoursSep, minutesUp, minutesDown, minutesSep, secsUp, secsDown, secondsSep, millisecsUp, millisecsDown, this);

		stopwatchTargetView.hideSetTargetTime();
	}

	public String	elapsedTime	= "0";

	public void start() {
		RemoteStopClockServiceInterface.getInstance().startStopClock();

		// this.startTime = System.currentTimeMillis();
		stopwatchView.switchStopwatchButton(ActivityID.STOPWATCH_RUNNING);

		startUpdates();

	}

	public void stop() {
		RemoteStopClockServiceInterface.getInstance().stopStopClock();
		stopwatchView.switchStopwatchButton(ActivityID.STOPCLOCK_NOT_RUNNING);
	}

	private void updateRunningStopClock() {
		if (targetIsSet() && isTargetTimeReached(elapsedTime)) {
			stop();
			stopwatchView.setStopClock(this.targetTime);
		} else {
			stopwatchView.setStopClock(elapsedTime);
		}
	}

	public boolean targetIsSet() {
		return this.targetIsSet;
	}

	public String getTargetTime() {
		return this.targetTime;
	}

	public void setTargetTime(String targetTime) {
		this.targetTime = targetTime;
	}

	public void setTargetCentiSecs(Integer centiSecs) {
		this.targetTime = StopwatchTools.setCentiSecs(centiSecs, this.targetTime);
	}

	public void setTargetSecs(Integer secs) {
		this.targetTime = StopwatchTools.setSecs(secs, this.targetTime);
	}

	public void setTargetMins(Integer mins) {
		this.targetTime = StopwatchTools.setMins(mins, this.targetTime);
	}

	public void setTargetHours(Integer hours) {
		this.targetTime = StopwatchTools.setHours(hours, this.targetTime);
	}

	private boolean isTargetTimeReached(String elapsedTime) {
		int stopClock = StopwatchTools.compare(this.targetTime, elapsedTime);

		return stopClock == StopwatchTools.EQUAL || stopClock == StopwatchTools.GREATER_THAN;
	}

	public void turnOffASetTargetTime() {
		this.targetIsSet = false;
	}

	public void turnOnASetTargetTime() {
		this.targetIsSet = true;
	}

	public void onResume() {
		if (RemoteStopClockServiceInterface.getInstance().isClockRunning()) {
			stopwatchView.switchStopwatchButton(ActivityID.STOPWATCH_RUNNING);
		} else {
			stopwatchView.switchStopwatchButton(ActivityID.STOPCLOCK_NOT_RUNNING);
		}
		updateStopClock();

	}

	private void startUpdates() {
		// send one message to kick of updates
		Message nextUpdateMsg = Message.obtain(updateHandler, ActivityID.UPDATESTOPCLOCK);
		updateHandler.sendMessageDelayed(nextUpdateMsg, 0);
	}

}
